<template>
  <div class="mod-user">
    <el-form :inline="true" :model="formData">
      <el-form-item label="订单日期">
        <el-date-picker
          @change="searchChange"
          size="small"
          v-model="formData.settlementDate"
          type="daterange"
          range-separator="至"
          placeholder="订单日期" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结算状态">
        <el-select size="small" v-model="formData.settlementStatus" @change="searchChange" clearable>
          <el-option v-for="(item, index) in settlementStatusList" :label="item.label" :value="item.value" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click.stop="searchChange">搜索</el-button>
      </el-form-item>
    </el-form>
    <div class="sumary-list">
      <div>订单金额: {{totalIncomeAmount}}</div>
      <div>订单数量: {{totalIncomeCount}}</div>
      <div>已结算金额: {{settlementIncomeAmount}}</div>
      <div>退款金额: {{totalRefundAmount}}</div>
      <div>退款订单数据: {{totalRefundCount}}</div>
    </div>
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :option="tableOption"
               @size-change="sizeChange"
               @current-change="currentChange">
      <template slot-scope="scope"
                slot="settlementStatus">
        <el-tag v-if="scope.row.settlementStatus == 1"
                size="small">已打款</el-tag>
        <el-tag type="warning" v-if="scope.row.settlementStatus == 0"
                size="small">未打款</el-tag>
      </template>
    </avue-crud>
  </div>
</template>

<script>
  import { tableOption } from '@/crud/settlement/index'
  export default {
    data () {
      return {
        dataList: [],
        dataListLoading: false,
        tableOption: tableOption,
        formData: {
          settlementStatus: '',
          settlementDate: '',
          startTime: '',
          endTime: ''
        },
        settlementStatusList: [
          {
            label: '全部',
            value: ''
          },
          {
            label: '未打款',
            value: 0
          },
          {
            label: '已打款',
            value: 1
          }
        ],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        totalIncomeAmount: 0,
        totalIncomeCount: 0,
        settlementIncomeAmount: 0,
        totalRefundAmount: 0,
        totalRefundCount: 0
      }
    },

    mounted () {
      this.getDataList()
    },

    methods: {
      // 获取数据列表
      getDataList (page, params) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/v1/merchant/system/get/settlement'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize
              },
              params
            )
          )
        }).then(({ data }) => {
          this.dataList = data.data.merchantCapitalSettlementIPage.records
          this.page.total = data.data.merchantCapitalSettlementIPage.total
          this.totalIncomeAmount = data.data.totalIncomeAmount
          this.totalIncomeCount = data.data.totalIncomeCount
          this.settlementIncomeAmount = data.data.settlementIncomeAmount
          this.totalRefundAmount = data.data.totalRefundAmount
          this.totalRefundCount = data.data.totalRefundCount
          this.dataListLoading = false
        })
      },

      // 条件查询
      searchChange () {
        if(this.formData.settlementDate&&this.formData.settlementDate.length > 0) {
          this.formData.startTime = this.formData.settlementDate[0]
          this.formData.endTime = this.formData.settlementDate[1]
        }
        else {
          this.formData.startTime = ''
          this.formData.endTime = ''
        }
        this.page.currentPage=1
        this.getDataList(this.page, this.formData)
      },

      update (id) {
        this.$confirm(`确定进行该操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl('/v1/merchant/system/payment'),
              method: 'post',
              data: this.$http.adornData({
                id: id,
                settlementStatus: 1
              })
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(this.page)
                }
              })
            })
          })
          .catch(() => { })
      },
      sizeChange(val){
        this.page.pageSize=val
        this.getDataList(this.page, this.formData)
      },
      currentChange(val){
        this.page.currentPage=val
        this.getDataList(this.page, this.formData)
      }
    }
  }
</script>

<style lang="scss">
  .remain{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    input{
      width: 100px;
    }
  }
  .sumary-list{
    display: flex;
    justify-content: flex-start;
    margin-bottom: 15px;
    div{
      height: 30px;
      padding: 0px 12px;
      line-height: 30px;
      border:solid 1px #009FF1;
      border-radius: 15px;
      margin-right: 10px;
    }
    div:hover{
      color: white;
      background-color: #009FF1;
    }
  }
</style>
